(function(t){BX.namespace("BX.UI");if(BX.UI.TileSelector){return}var e=[];function i(t){this.id=t.id;this.name=t.name||null;this.node=t.node;this.data=t.data;this.removeNode=null;this.nameNode=o.getNode("tile-item-name",this.node);if(!this.name){this.name=this.nameNode.textContent}}i.prototype.changeRemoving=function(t){if(!this.removeNode){return}this.removeNode.style.display=t?"":"none"};function n(t){this.init(t)}n.prototype.events={containerClick:"container-click",tileClick:"tile-click",tileRemove:"tile-remove",tileEdit:"tile-edit",tileAdd:"tile-add",buttonAdd:"add",buttonSelect:"select",buttonSelectFirst:"select-first",search:"search",input:"input",searcherCategoryClick:"popup-category-click",searcherItemClick:"popup-item-click",searcherInit:"popup-search-init"};n.getById=function(t){var i=e.filter(function(e){return e.id===t&&document.body.contains(e.context)});return i.length>0?i[0]:null};n.getList=function(){return e};n.prototype.init=function(t){this.list=[];this.id=t.id;this.context=BX(t.containerId);this.duplicates=t.duplicates;this.multiple=t.multiple;this.readonly=t.readonly;this.manualInputEnd=t.manualInputEnd;this.caption=t.caption;this.captionMore=t.captionMore;this.tilesLimit=!!t.tilesLimit?parseInt(t.tilesLimit):10;this.attributeId="data-bx-id";this.attributeData="data-bx-data";this.tileContainer=o.getNode("tile-container",this.context);this.tileTemplate=o.getNode("tile-template",this.context);this.input=o.getNode("tile-input",this.context);this.buttonAdd=o.getNode("tile-add",this.context);this.buttonSelect=o.getNode("tile-select",this.context);this.buttonMore=o.getNode("tile-more",this.context);if(!this.context||!this.input){return}o.getNodes("tile-item",this.context).forEach(this.initNode.bind(this));if(!this.readonly){this.initEventHandlers()}this.searcher=null;e.push(this)};n.prototype.initEventHandlers=function(){if(this.buttonAdd){BX.bind(this.buttonAdd,"click",this.onButtonAdd.bind(this))}if(this.context){BX.bind(this.context,"click",this.onContainerClick.bind(this))}if(this.buttonSelect){BX.bind(this.buttonSelect,"click",this.onButtonSelect.bind(this));BX.bind(this.tileContainer,"click",this.onButtonSelect.bind(this))}BX.bind(this.input,"input",this.onInput.bind(this));if(this.buttonMore){BX.bind(this.buttonMore,"click",this.onButtonMore.bind(this))}if(!this.manualInputEnd){BX.bind(this.input,"blur",this.onInputEnd.bind(this));o.handleKeyEnter(this.input,this.onInputEnd.bind(this))}BX.bind(this.input,"keydown",function(t){if(t.key==="Enter"){t.preventDefault();t.stopPropagation();return false}})};n.prototype.getSearchInput=function(){return this.input};n.prototype.isSearcherInit=function(){return!!this.searcher};n.prototype.clearSearcher=function(){this.isButtonSelectFired=false;if(this.searcher){this.searcher.hide();this.searcher=null}};n.prototype.hideSearcher=function(){this.searcher.hide()};n.prototype.showSearcher=function(t){if(!this.searcher){this.searcher=new s({id:this.id,caller:this,context:this.context,title:t||""});this.fire(this.events.searcherInit,[this.searcher])}this.searcher.filterByName();this.searcher.show()};n.prototype.setSearcherData=function(t){if(!this.searcher){this.showSearcher()}this.searcher.setCategories(t)};n.prototype.initNode=function(t){if(!t){return null}var e=t.getAttribute(this.attributeId);var n=t.getAttribute(this.attributeData);try{n=JSON.parse(n)}catch(t){try{n=JSON.parse(BX.util.htmlspecialcharsback(n))}catch(t){n={}}}var s=new i({id:e,node:t,data:n});if(s.id&&!this.duplicates&&this.findDuplicates(s.id)){s=null;return null}s.removeNode=o.getNode("remove",t);if(s.removeNode){BX.bind(s.removeNode,"click",this.onRemove.bind(this,s))}BX.bind(t,"click",this.onClick.bind(this,s));this.list.push(s);return s};n.prototype.onRemove=function(t,e){e.preventDefault();e.stopPropagation();this.removeTile(t);if(BX.UI.SelectorManager){var i=BX.UI.SelectorManager.instances[this.id];if(i&&i.callback.unSelect){if(BX.type.isNotEmptyObject(t.data)&&BX.type.isNotEmptyString(t.data.entityType)){i.callback.unSelect({item:i.entities[t.data.entityType.toUpperCase()].items[t.id.match(/^\d+$/)?"U"+t.id:t.id],entityType:t.data.entityType,selectorId:i.id})}}}return false};n.prototype.onClick=function(t,e){e.preventDefault();e.stopPropagation();this.fire(this.events.tileClick,[t])};n.prototype.removeTiles=function(){var t=this.list;t.forEach(this.removeTile.bind(this))};n.prototype.removeTile=function(t){this.list=BX.util.deleteFromArray(this.list,this.list.indexOf(t));BX.remove(t.node);this.fire(this.events.tileRemove,[t]);this.recalcButtonSelectText();if(this.buttonMore.style.display!="none"){this.recalcMore()}};n.prototype.recalcMore=function(){if(this.checkTilesLimit({action:"remove"})){this.buttonMore.style.display="none"}o.getNodes("tile-item",this.context).forEach(function(t,e){t.style.display=e>=this.tilesLimit?"none":""}.bind(this))};n.prototype.getTile=function(t){var e=this.list.filter(function(e){return e.id===t});return e.length>0?e[0]:null};n.prototype.getTilesData=function(){return this.list.map(function(t){return t.data})};n.prototype.getTilesId=function(){return this.list.map(function(t){return t.id}).filter(function(t){return!!t})};n.prototype.getTiles=function(){return this.list};n.prototype.findDuplicates=function(t){var e=this.getTile(t);if(!e){return false}this.removeTile(e)};n.prototype.addTile=function(t,e,i,n,s){if(!t||this.readonly){return null}i=i||"";if(!this.multiple){if(this.isSearcherInit()){this.hideSearcher()}if(this.list.length>0){var r=this.list[0];if(r&&r.id==i){return}}this.removeTiles()}e=e||{};s=s||"";n=n||"";var a=this.tileTemplate;if(!a){return null}a=a.innerHTML;var l="";if(s){l+="color: "+BX.util.htmlspecialchars(s)+"; "}if(n){l+="background-color: "+BX.util.htmlspecialchars(n)+"; "}if(BX.type.isNotEmptyString(e.state)&&e.state=="init"){l+=this.checkTilesLimit({action:"init"})?"":"display: none;"}var c=BX.type.isNotEmptyString(e.entityType)?e.entityType.toLowerCase():"none";if(!!e.extranet){c+="-extranet"}if(!!e.crmEmail){c+="-crm"}a=o.replace(a,{id:BX.util.htmlspecialchars(i+""),name:BX.util.htmlspecialchars(t),data:BX.util.htmlspecialchars(JSON.stringify(e)),style:l,type:c,readonly:!!e.readonly?"yes":"no"},true);var h=document.createElement("div");h.innerHTML=a;h=h.children[0];var u=this.initNode(h);if(!u){return null}this.buttonMore.parentNode.insertBefore(h,this.buttonMore);this.fire(this.events.tileAdd,[u]);this.recalcButtonSelectText();return u};n.prototype.updateTile=function(t,e,i,n,o){if(!t||this.readonly){return null}e=e||null;i=i||null;n=n||null;o=o||null;if(e){t.nameNode.textContent=e}if(i){t.data=i}if(n||n===null){t.node.style.backgroundColor=n}if(o){t.node.style.color=o}this.fire(this.events.tileEdit,[t]);return t};n.prototype.checkTilesLimit=function(t){var e=true,i=o.getNodes("tile-item",this.context).length,n=BX.type.isNotEmptyObject(t)&&BX.type.isNotEmptyString(t.action)?t.action:null;if(i>=this.tilesLimit){e=false;this.buttonMore.style.display="";this.buttonMore.title=BX.message("UI_TILE_SELECTOR_MORE").replace("#NUM#",i-this.tilesLimit+(n=="init"?1:0))}return e};n.prototype.fire=function(t,e){BX.onCustomEvent(this,t,e)};n.prototype.onInput=function(){var t=this.input.value;if(this.searcher&&t.length>0){this.searcher.filterByName(t)}this.fire(this.events.input,[this.input.value])};n.prototype.onInputEnd=function(){var t=this.input.value;this.input.value="";o.changeDisplay(this.input,false);o.changeDisplay(this.buttonSelect,true);this.recalcButtonSelectText();this.fire(this.events.search,[t])};n.prototype.onButtonAdd=function(t){t.preventDefault();t.stopPropagation();this.fire(this.events.buttonAdd,[])};n.prototype.onContainerClick=function(){this.fire(this.events.containerClick,[])};n.prototype.onButtonSelect=function(t){t.preventDefault();t.stopPropagation();o.changeDisplay(this.buttonSelect,false);o.changeDisplay(this.input,true);this.input.focus();this.fire(this.events.buttonSelect,[]);if(!this.isButtonSelectFired){this.fire(this.events.buttonSelectFirst,[]);this.isButtonSelectFired=true}};n.prototype.onButtonMore=function(t){t.preventDefault();t.stopPropagation();o.getNodes("tile-item",this.context).forEach(function(t){t.style.display=""});t.currentTarget.style.display="none"};n.prototype.recalcButtonSelectText=function(){if(!this.buttonSelect){return}var t=this.getTiles();this.buttonSelect.innerHTML=t.length>0?this.captionMore:this.caption};var o={getObjectByKey:function(t,e,i){var n=t.filter(function(t){return t.hasOwnProperty(e)&&t[e]===i});return n.length>0?n[0]:null},getNode:function(t,e){var i=this.getNodes(t,e);return i.length>0?i[0]:null},getNodes:function(t,e){if(!e){return[]}return BX.convert.nodeListToArray(e.querySelectorAll('[data-role="'+t+'"]'))},changeClass:function(t,e,i){if(!t){return}if(i){BX.addClass(t,e)}else{BX.removeClass(t,e)}},changeDisplay:function(t,e){if(!t){return}t.style.display=e?"":"none"},replace:function(t,e,i){e=e||{};i=i||false;if(!t){return""}for(var n in e){if(!e.hasOwnProperty(n)){continue}var o=e[n];o=o||"";if(!i&&o){o=BX.util.htmlspecialchars(o)}t=t.replace(new RegExp("%"+n+"%","g"),o)}return t},handleKeyEnter:function(e,i){if(!i){return}var n=function(e){e=e||t.event;if(e.keyCode===10||e.keyCode===13){e.preventDefault();e.stopPropagation();i();return false}};BX.bind(e,"keyup",n)},getTemplatedNode:function(t,e,i){if(!t){return null}var n=o.replace(t.innerHTML,e,i);var s=document.createElement("div");s.innerHTML=n;return s.children[0]}};function s(t){this.init(t)}s.prototype.classNameCategoryActive="ui-tile-selector-searcher-sidebar-item-selected";s.prototype.classNameItemActive="ui-tile-selector-searcher-content-item-selected";s.prototype.init=function(t){this.id=t.id;this.context=t.context;this.caller=t.caller;this.categories=[];this.items=[];this.currentCategory=null;this.categoryTemplate=o.getNode("popup-category-template",this.context);this.itemTemplate=o.getNode("popup-item-template",this.context);this.content=o.getTemplatedNode(o.getNode("popup-template",this.context));this.loader=o.getNode("popup-loader",this.content);this.categoryContainer=o.getNode("popup-category-list",this.content);this.itemContainer=o.getNode("popup-item-list",this.content);this.itemContainer.innerHTML="";this.categoryContainer.innerHTML="";this.title=o.getNode("popup-title",this.content);if(this.title){this.title.textContent=t.title}if(t.dataList){this.setCategories(t.dataList)}BX.addCustomEvent(this.caller,this.caller.events.tileAdd,this.onTileAdd.bind(this));BX.addCustomEvent(this.caller,this.caller.events.tileRemove,this.onTileRemove.bind(this))};s.prototype.onTileAdd=function(t){var e=o.getObjectByKey(this.items,"id",t.id);if(!e){return}o.changeClass(e.node,this.classNameItemActive,true)};s.prototype.onTileRemove=function(t){var e=o.getObjectByKey(this.items,"id",t.id);if(!e){return}o.changeClass(e.node,this.classNameItemActive,false)};s.prototype.filterByName=function(t){t=t||"";if(t.length<3){o.changeDisplay(this.categoryContainer,true);this.setCurrentCategory();return}var e=new RegExp(BX.util.escapeRegExp(t),"i");this.items.forEach(function(t){o.changeDisplay(t.node,e.test(t.name))});o.changeDisplay(this.categoryContainer,false)};s.prototype.onCategoryClick=function(t){this.setCurrentCategory(t);this.caller.fire(this.caller.events.searcherCategoryClick,[t])};s.prototype.setCurrentCategory=function(t){t=t||this.categories[0];if(this.currentCategory){BX.removeClass(this.currentCategory.node,this.classNameCategoryActive)}this.currentCategory=t;if(!t){return}BX.addClass(this.currentCategory.node,this.classNameCategoryActive);this.items.forEach(function(e){o.changeDisplay(e.node,e.category===t)})};s.prototype.onItemClick=function(t){this.caller.addTile(t.name,t.data,t.id,t.bgcolor,t.color);this.caller.fire(this.caller.events.searcherItemClick,[t])};s.prototype.getCategory=function(t){return o.getObjectByKey(this.categories,"id",t)};s.prototype.getItem=function(t){return o.getObjectByKey(this.items,"id",t)};s.prototype.updateItem=function(t,e,i){if(e){t.node.textContent=e;t.node.title=e}if(i){t.data=i}};s.prototype.addItem=function(t,e,i,n){var s=o.getTemplatedNode(this.itemTemplate,{name:i});var r={category:t,node:s,id:e,name:i,data:n||{}};this.items.push(r);this.itemContainer.appendChild(s);BX.bind(s,"click",this.onItemClick.bind(this,r));return r};s.prototype.setItems=function(t,e){this.items=[];e.forEach(function(e){this.addItem(t,e.id,e.name,e.data)},this)};s.prototype.addItems=function(t,e){e.forEach(function(e){this.addItem(t,e.id,e.name,e.data)},this)};s.prototype.addCategory=function(t,e,i,n){var s=o.getTemplatedNode(this.categoryTemplate,{name:e});var r={node:s,id:t,name:e,data:i||{}};this.categories.push(r);this.categoryContainer.appendChild(s);BX.bind(s,"click",this.onCategoryClick.bind(this,r));this.addItems(r,n);return r};s.prototype.setCategories=function(t){this.items=[];this.categories=[];this.itemContainer.innerHTML="";this.categoryContainer.innerHTML="";t.forEach(function(t){this.addCategory(t.id,t.name,t.data,t.items)},this);if(this.categories.length>0){this.setCurrentCategory(this.categories[0])}var e=this.caller.getTilesId();this.items.filter(function(t){var i=t.id&&BX.util.in_array(t.id,e);o.changeClass(t.node,this.classNameItemActive,i)},this);o.changeDisplay(this.loader,false);o.changeDisplay(this.itemContainer,true);o.changeDisplay(this.categoryContainer,true)};s.prototype.showLoader=function(){o.changeDisplay(this.loader,true)};s.prototype.hide=function(){if(!this.popup){return}this.popup.close()};s.prototype.show=function(){if(this.popup){this.popup.show();return}this.popup=BX.Main.PopupManager.create(this.id,this.context,{width:620,height:290,autoHide:true,lightShadow:true,closeByEsc:true,closeIcon:false,offsetLeft:40,angle:true,buttons:[new BX.UI.CloseButton({onclick:function(){this.popup.close()}.bind(this)})]});this.popup.setContent(this.content);o.changeDisplay(this.content,true);this.popup.show()};BX.UI.TileSelector=n;BX.addCustomEvent("BX.Main.SelectorV2:onGetDataStart",function(t){var e=BX("ui-tile-selector-"+t+"-mask");if(!e){return}e.classList.add("ui-tile-selector-selector-mask-active")});BX.addCustomEvent("BX.Main.SelectorV2:onGetDataFinish",function(t){var e=BX("ui-tile-selector-"+t+"-mask");if(!e){return}e.classList.remove("ui-tile-selector-selector-mask-active")})})(window);
//# sourceMappingURL=script.map.js